package com.luych.toolbox.gateway.center.websocket;

import javax.websocket.Session;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WebSocketCache {

    private static final Map<String, Session> sessions = new ConcurrentHashMap<>();

    public static Session get(String userId) {
        return sessions.get(userId);
    }

    public static void set(String userId, Session session) {
        sessions.put(userId, session);
    }

    public static void del(String userId) {
        sessions.remove(userId);
    }

    public static String getUserIdBySessionId(String sessionId) {
        for (Map.Entry<String, Session> entry : sessions.entrySet()) {
            if (entry.getValue().getId().equals(sessionId)) {
                return entry.getKey();
            }
        }
        return null;
    }

    public static Map<String, Session> getSessions() {
        return sessions;
    }
}
